
using System;
using finLasku;

namespace finlasku
{
	public delegate void RowUpdatedEventHandler(int row);
	
	/// <summary>
	/// Holds all data used by the application and allows UI to access business logic.
	/// </summary>	
	public class AppContext
	{
		private IInvoiceManager _invoiceManager = null;
		private ICustomerManager _customerManager = null;
		private Invoice _currentinvoice = null;
		public event RowUpdatedEventHandler Rowupdated;
		
		
		public AppContext()
		{
		}
		
		public Invoice CurrentInvoice {
			get{
				return _currentinvoice;
			}
			set {
				_currentinvoice = value;
			}
		}
		
		public void UpdateInvoicerow(InvoiceRow row, int rownumber) {
			//_currentinvoice.Rows[rownumber].TaxFreePrice = _currentinvoice.Rows[rownumber].Quantity * _currentinvoice.Rows[rownumber].Price;
			row.TaxFreePrice = row.Quantity * row.Price;
			
			if(_currentinvoice == null) {
				_currentinvoice = new Invoice();
			}
			
			if(_currentinvoice.Rows.Count <= rownumber) {
				_currentinvoice.Rows.Add(row);
			} else {
				_currentinvoice.Rows[rownumber] = row;
			}
			
			if(Rowupdated != null) {
				Rowupdated(rownumber);
			}
		}
		
		public void UpdateTitle(string title){
			if(_currentinvoice == null) {
				_currentinvoice = new Invoice();
			}
			_currentinvoice.Title = title;
		}
	}
}
